package com.qf.ability.search.listener;

import com.qf.ability.search.entity.GoodsGuigeSearch;
import com.qf.ability.search.entity.GoodsInfoSearch;
import com.qf.ability.search.service.IGoodsSearch;
import com.qf.commons.event.apply.consumer.EventHandler;
import com.qf.commons.event.apply.consumer.IEventHandler;
import com.qf.commons.event.entity.QphoneMessage;
import com.qf.data.base.utils.QfBeanUtils;
import com.qf.data.goods.dto.GoodsInfoDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
@EventHandler("goods_insert")
@Slf4j
@Component
public class GoodsInsertListener implements IEventHandler<GoodsInfoDto> {

    @Autowired
    private IGoodsSearch goodsSearch;

    @Override
    public void handler(GoodsInfoDto goodsDto, QphoneMessage qphoneMessage) {
        log.info("[event goods_insert] - 接收到商品添加的事件 - {}", goodsDto);
        //转换
        GoodsInfoSearch goodsInfoSearch = QfBeanUtils.beanCopy(goodsDto, GoodsInfoSearch.class);
        List<GoodsGuigeSearch> goodsGuigeSearches = QfBeanUtils.listCopy(goodsDto.getGuiges(), GoodsGuigeSearch.class);
        goodsInfoSearch.setGuiges(goodsGuigeSearches);
        log.info("[event goods_insert] - 转换dto实体对象 - {}", goodsInfoSearch);
        goodsSearch.insert(goodsInfoSearch);
    }

}
